package com.uit.smarttool.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.uit.smarttool.dto.KhachHang;
import com.uit.smarttool.ultility.CallSoap;

public final class Tab3Activity extends Activity {

	TextView tvcustaikhoan, tvcusdiemthuong, tvcustenloaikh, tvcusuudai,
			tvcussotien;
	EditText edtcusdiachi, edtcusdienthoai, edtcushoten, edtcuscmnd,
			edtcusemail;
	KhachHang khachhang;
	static String result = "";
	static String resultLoaikh = "";
	static String resultkhachhang = "";
	Button btnNapTien, btnNangCap, btnCapNhat;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layoutcustomer);
		edtcuscmnd = (EditText) findViewById(R.id.edtcuscmnd);
		tvcusdiemthuong = (TextView) findViewById(R.id.tvcusdiemthuong);
		edtcusemail = (EditText) findViewById(R.id.edtcusemail);
		edtcushoten = (EditText) findViewById(R.id.edtcushoten);
		tvcussotien = (TextView) findViewById(R.id.tvcussotien);
		tvcustaikhoan = (TextView) findViewById(R.id.tvcustaikhoan);
		tvcustenloaikh = (TextView) findViewById(R.id.tvcustenloaikh);
		tvcusuudai = (TextView) findViewById(R.id.tvcusuudai);
		edtcusdiachi = (EditText) findViewById(R.id.edtcusdiachi);
		edtcusdienthoai = (EditText) findViewById(R.id.edtcusdienthoai);

		btnCapNhat = (Button) findViewById(R.id.btnCusCapNhat);
		btnNangCap = (Button) findViewById(R.id.btnCusNangCap);
		btnNapTien = (Button) findViewById(R.id.btnCusNapTien);


		getKhachHang c = new getKhachHang();
		try {
			result = "START";
			c.join();
			c.start();
			while (result == "START") {
				try {
					Thread.sleep(10);

				} catch (Exception ex) {
				}
			}
		} catch (Exception e) {
		}

		edtcuscmnd.setText(String.valueOf(khachhang.getcMND()));
		tvcusdiemthuong.setText(String.valueOf(khachhang.getDiemThuong()));
		edtcusemail.setText(khachhang.getEmail());
		edtcushoten.setText(khachhang.getHoTen());
		tvcussotien.setText(String.valueOf(khachhang.getSoTien()));
		tvcustaikhoan.setText(khachhang.getTaiKhoan());
		tvcustenloaikh.setText(khachhang.getTenloaikh());
		tvcusuudai.setText(khachhang.getUudai());
		edtcusdiachi.setText(khachhang.getDiaChi());
		edtcusdienthoai.setText(String.valueOf(khachhang.getDienThoai()));
		
		btnNapTien.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(v.getContext(), AddMoneyActivity.class);
				startActivity(myIntent);
			}
		});

		btnCapNhat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setKhachHang c=new setKhachHang();
				try {
					resultkhachhang = "START";
					c.join();
					c.start();
					while (resultkhachhang == "START") {
						try {
							Thread.sleep(10);

						} catch (Exception ex) {
						}
					}
				} catch (Exception e) {
				}
			}
		});

		btnNangCap.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (tvcustenloaikh.getText().toString().equals("VIP")) {
					Toast.makeText(getApplicationContext(),
							"Tài khoản đã được nâng cấp tối đa.", 3000).show();
				} else {

					if (Integer.valueOf(tvcusdiemthuong.getText().toString()) >= 20) {
						setLoaikh c = new setLoaikh();
						try {
							resultLoaikh = "START";
							c.join();
							c.start();
							while (resultLoaikh == "START") {
								try {
									Thread.sleep(10);

								} catch (Exception ex) {
								}
							}
						} catch (Exception e) {
						}
						Toast.makeText(
								getApplicationContext(),
								"Nâng cấp tài khoản thành công. Xin vui lòng thoát để cập nhật.",
								3000).show();
					} else if (Integer.valueOf(tvcusdiemthuong.getText()
							.toString()) < 20) {
						Toast.makeText(getApplicationContext(),
								"Tài khoản trên 20 điểm mới dược nâng cấp!",
								3000).show();
					}

				}
			}
		});

	}

	// get newest statue of khachhang
	private class getKhachHang extends Thread {

		public void run() {
			try {
				CallSoap cs = new CallSoap();
				khachhang = cs.selectCustomerByUser(LoginActivity.user,
						LoginActivity.pass);
				Tab3Activity.result = "Finish";
			} catch (Exception ex) {
				Tab3Activity.result = ex.toString();
			}

		}
	}

	// get newest statue of khachhang
	private class setLoaikh extends Thread {

		public void run() {
			try {
				CallSoap cs = new CallSoap();
				cs.setMaLoai(String.valueOf(LoginActivity.user_info.getMakh()));
				cs.resetDiemThuongKH(String.valueOf(LoginActivity.user_info.getMakh()));
				Tab3Activity.resultLoaikh = "Finish";
			} catch (Exception ex) {
				Tab3Activity.resultLoaikh = ex.toString();
			}

		}
	}

	// set khachhang
	private class setKhachHang extends Thread {

		public void run() {
			try {
				CallSoap cs = new CallSoap();
				cs.setKhachhang(String.valueOf(LoginActivity.user_info
						.getMakh()), edtcushoten.getText().toString(),
						edtcusdiachi.getText().toString(), Integer
								.valueOf(edtcusdienthoai.getText().toString()),
						edtcusemail.getText().toString(), Integer
								.valueOf(edtcuscmnd.getText().toString()));
				Tab3Activity.resultkhachhang = "Finish";
			} catch (Exception ex) {
				Tab3Activity.resultkhachhang = ex.toString();
			}

		}
	}
}
